<template>
  <div class="coupon">
    <h1>系统积分</h1>
    <hr />
    <el-row class="points-add">
      <el-button icon="el-icon-plus" type="primary" @click="pointsAdd"
        >添加系统积分</el-button
      >
    </el-row>
    <el-table :data="systemPointsList" style="width: 100%">
      <el-table-column prop="id" label="编号" width="100"> </el-table-column>
      <el-table-column prop="name" label="积分名称" width="120">
      </el-table-column>
      <el-table-column prop="giveType" label="发放方式" width="250">
      </el-table-column>
      <el-table-column prop="number" label="数量"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button size="mini" @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="banner-page"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="totalCurrent"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      systemPointsList: [],
      currentPage: 1,
      pageSize: 10,
      totalCurrent: 0,
    };
  },
  methods: {
    //编辑
    handleEdit(index, row) {
      console.log(index, row);

      this.$router.push({
        path: "/cpscore/score/sysscore/add",
        query: { row: row },
      });
    },
    //删除
    handleDelete(index, row) {
      console.log(index, row);

      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$http({
            url: "/systempoint/deleteSystemPoint",
            method: "POST",
            params: {
              id: row.id,
            },
          })
            .then((res) => {
              console.log("请求成功：", res);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getInit(this.currentPage, this.pageSize);
            })
            .catch((err) => {
              console.log("请求失败：", err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增
    pointsAdd() {
      this.$router.push("/cpscore/score/sysscore/add");
    },
    //列表数据渲染
    getInit(currentPage, pageSize) {
      this.$http({
        url: "/systempoint/selectByPage",
        method: "GET",
        params: {
          currentPage,
          pageSize,
        },
      })
        .then((res) => {
          console.log("返回数据", res);
          let points = res.data.data.list;

          for (let j = 0; j < points.length; j++) {
            //发放方式处理
            switch (points[j].giveType) {
              case 1:
                points[j].giveType = "系统发放";
                break;
              case 2:
                points[j].giveType = "公司举行活动发放";
                break;
              case 3:
                points[j].giveType = "第三方";
                break;
              default:
                points[j].giveType = "系统发放";
            }
          }
          this.systemPointsList = points;
          this.totalCurrent = res.data.data.total;
        })
        .catch((err) => {
          console.log("获取列表数据失败：", err);
        });
    },
    //分页重新渲染数据
    currentChange(currentPage) {
      console.log("当前页：", currentPage);
      this.currentPage = currentPage;
      this.getInit(currentPage, this.pageSize);
    },
  },
  created() {
    this.getInit(this.currentPage, this.pageSize);
  },
};
</script>

<style lang="scss" scoped>
h1 {
  margin-bottom: 10px;
}

.el-row {
  margin: 10px 0;
}

.el-pagination {
  text-align: center;
  margin-top: 40px;
}
</style>